package com.example.administrator.hqq.adapter.discover;


import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.comment.CommentBean;
import com.example.administrator.hqq.View.comment.CommentTextView;
import com.example.administrator.hqq.View.comment.UserBean;
import com.example.administrator.hqq.bean.post.PostDetailsCommentBean;
import com.example.administrator.hqq.utils.TimeUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/21 14:56
 * @remark:
 */
public class PostCommentAdapter extends RecyclerView.Adapter<PostCommentAdapter.ViewHolder> {
    private Context context;
    private List<PostDetailsCommentBean.DataBean.ReplyBean> strings;
    private OnPostCommentClick onPostCommentClick;
    public PostCommentAdapter(Context context, List<PostDetailsCommentBean.DataBean.ReplyBean> strings) {
        this.context = context;
        this.strings = strings;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.user_pic)
        ImageView userPic;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.comment_context)
        TextView commentContext;
        @BindView(R.id.inform)
        AutoRelativeLayout inform;
        @BindView(R.id.comment1)
        CommentTextView comment1;
        @BindView(R.id.comment2)
        CommentTextView comment2;
        @BindView(R.id.comment3)
        CommentTextView comment3;
        @BindView(R.id.comment4)
        CommentTextView comment4;
        @BindView(R.id.comment_time)
        TextView commentTime;
        @BindView(R.id.comment_child)
        ImageView commentChild;
        @BindView(R.id.comment_give_like)
        ImageView commentGiveLike;
        @BindView(R.id.comment_number)
        TextView commentNumber;
        @BindView(R.id.comment_auto)
        AutoLinearLayout commentAuto;
        private View view;
        private List<CommentTextView> commentTextViews;
        public ViewHolder(View itemView) {
            super(itemView);
            view=itemView;
            ButterKnife.bind(this, itemView);
            commentTextViews=new ArrayList<>();
            commentTextViews.add(comment1);
            commentTextViews.add(comment2);
            commentTextViews.add(comment3);
            commentTextViews.add(comment4);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.post_comment_list_item, null);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(strings.get(position).getUser_portrait_url()).transform(new CenterCrop(context),new GlideCircleTransform(context)).into(holder.userPic);
        holder.userName.setText(strings.get(position).getUser_nickname());
        holder.commentContext.setText(strings.get(position).getText());
        for (int i = 0; i < holder.commentTextViews.size(); i++) {
            holder.commentTextViews.get(i).setVisibility(View.GONE);
        }
        if (strings.get(position).getDiscussion()==null||strings.get(position).getDiscussion().size()==0){
            holder.commentAuto.setVisibility(View.GONE);
        }else{
            holder.commentAuto.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < strings.get(position).getDiscussion().size(); i++) {
            if (i<4)
            {
                setCommentText(holder.commentTextViews.get(i),position,i);
            }
        }
        if (strings.get(position)!=null)
        {
            holder.commentNumber.setText(strings.get(position).getLike_amount()==0?"":strings.get(position).getLike_amount()+"");
            holder.commentGiveLike.setSelected(strings.get(position).getLike_state()==0?false:true);
        }
        holder.commentTime.setText(TimeUtils.getTimeString(strings.get(position).getTime()));
        setOnclick(holder,position);
    }

    private void setOnclick(final ViewHolder holder, final int position) {
        holder.inform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onPostCommentClick!=null)
                {
                    onPostCommentClick.informOnclick(position);
                }
            }
        });
        holder.commentGiveLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onPostCommentClick!=null)
                {
                    onPostCommentClick.giveLikeOnclick(position, holder.commentGiveLike);
                }
            }
        });
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onPostCommentClick!=null)
                {
                    onPostCommentClick.detailsOnclick(position);
                }
            }
        });
    }

    private void setCommentText(CommentTextView comment1, int position, int i) {
        comment1.setVisibility(View.VISIBLE);
        CommentBean commentBean = new CommentBean();
        commentBean.setComment(strings.get(position).getDiscussion().get(i).getText());
        UserBean commentUser = new UserBean();
        commentUser.setUserName(strings.get(position).getDiscussion().get(i).getUser_nickname());
        commentBean.setUser(commentUser);
        comment1.setText(commentBean);
    }

    @Override

    public int getItemCount() {
        return strings.size();
    }
    public interface OnPostCommentClick{
        void giveLikeOnclick(int position,ImageView postLick);
        void informOnclick(int position);
        void detailsOnclick(int position);
    }
    public void setOnPostCommentClick(OnPostCommentClick onPostCommentClick) {
        this.onPostCommentClick = onPostCommentClick;
    }
}
